package com.zls;

import com.zls.dao.SpitRepoistory;
import com.zls.model.Spit;
import com.zls.utils.IdWorker;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes =MongoApp.class)
public class Demo {
    @Autowired
    private SpitRepoistory spitRepoistory;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;

    /**
     * 查询所有
     */
    @Test
    public void demo01(){
        List<Spit> all = spitRepoistory.findAll();
        all.stream().forEach(o-> System.out.println(o));
    }
    /**
     * 根据id查询
     */
    @Test
    public void demo02(){
        String id="1";
        Spit spit = spitRepoistory.findById(id).get();
        System.out.println(spit);
        Criteria criteria = Criteria.where("userid").in("1014", "1013", "1015").and("visits").gt(1000);
        List<Spit> spits = mongoTemplate.find(new Query(criteria), Spit.class);
        for (Spit spit1 : spits) {
            System.out.println(spit1);
        }

    }
    /**
     * 添加
     */
    @Test
    public void demo03(){
        Spit spit = new Spit();
        spit.set_id(idWorker.nextId()+"");
        Spit save = spitRepoistory.save(spit);
        System.out.println(save);
    }
    /**
     *修改
     */
    @Test
    public void demo04(){
        Spit spit = spitRepoistory.findById("1").get();
        spit.setUserid("10086100");
        Spit save = spitRepoistory.save(spit);
        System.out.println(save);
    }
    /**
     *删除
     */
    @Test
    public void demo05(){
        Spit spit = spitRepoistory.findById("1547513286252036096").get();
        spitRepoistory.delete(spit);
    }
}
